from lib import ui
from lib import gcnf
import gtk
import unittest

class Icon(gtk.VBox):
    def toggled(self, *w):
        active = self.checkbutton.get_active()
        self.image.set_sensitive(active)
    def button_clicked(self, *w):
        active = self.checkbutton.get_active()
        self.checkbutton.set_active(not active)
        self.checkbutton.emit('toggled')
    def __init__(self, icon_name, visible_key, name_key, default_name):
        assert isinstance(icon_name, str)
        assert isinstance(visible_key, str)
        assert isinstance(name_key, str)
        self.image = ui.image(icon_name, 64)
        self.icon_name = gcnf.get_string(name_key) or default_name
        self.checkbutton = gcnf.CheckButton(self.icon_name, visible_key)
        self.checkbutton.connect('toggled', self.toggled)
        
        imagebox = gtk.VBox(False, 5)
        imagebox.pack_start(self.image, False)
        imagebox.pack_start(self.checkbutton, False)
        
        imagebutton = gtk.Button()
        imagebutton.set_can_focus(False)
        imagebutton.set_relief(gtk.RELIEF_NONE)
        imagebutton.add(imagebox)
        imagebutton.connect('clicked', self.button_clicked)
        
        gtk.VBox.__init__(self, False, 5)
        self.pack_start(imagebutton, False)
        self.toggled()

def _key(key):
    return '/apps/nautilus/desktop/' + key

class UI(gtk.VBox):
    def table(self):
        computer = Icon('computer', _key('computer_icon_visible'), _key('computer_icon_name'), _('computer'))
        home = Icon('user-home', _key('home_icon_visible'), _key('home_icon_name'), _('home folder'))
        trash = Icon('user-trash', _key('trash_icon_visible'), _key('trash_icon_name'), _('trash icon'))
        network = Icon('network-workgroup', _key('network_icon_visible'), _key('network_icon_name'), _('network server'))
        table = gtk.Table()
        table.set_homogeneous(True)
        table.set_border_width(10)
        table.set_row_spacings(20)
        table.set_col_spacings(20)
        table.attach(computer, 0, 1, 0, 1, gtk.FILL)
        table.attach(home, 1, 2, 0, 1, gtk.FILL)
        table.attach(trash, 0, 1, 1, 2, gtk.FILL)
        table.attach(network, 1, 2, 1, 2, gtk.FILL)
        return table
    def __init__(self):
        gtk.VBox.__init__(self, False, 0)
        self.set_border_width(10)
        label = gtk.Label()
        label.set_markup('<b>%s</b>' % _('Select visible icons'))
        label.set_alignment(0, 0)
        self.pack_start(label, False)
        self.pack_start(self.table(), False)

def run():
    gtk.window_set_default_icon_name('preferences-desktop')
    dialog = gtk.Dialog(title=_('Desktop Properties'), parent=None, flags=gtk.DIALOG_NO_SEPARATOR, buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
    dialog.set_resizable(False)
    dialog.set_default_response(gtk.RESPONSE_CLOSE)
    dialog.vbox.pack_start(UI(), False)
    dialog.show_all()
    dialog.run()
    dialog.destroy()

class _test(unittest.TestCase):
    def test(self):
        run()
